{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8b216130",
   "metadata": {},
   "source": [
    "**礼仪的week13**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c93fb2e7",
   "metadata": {},
   "source": [
    "# 字典 （dict）key:value"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "942c163e",
   "metadata": {},
   "source": [
    "* 无序的数据结构\n",
    "* 利用键值来索引数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1134f95",
   "metadata": {},
   "outputs": [],
   "source": [
    "#批量增加键值对\n",
    "dict_01={}\n",
    "keys=['name','age','num','home']\n",
    "values=['criska','19','2110120088','巴西']\n",
    "dict_01=dict(zip(keys,values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "59937c4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'criska', 'age': '19', 'num': '2110120088', 'home': '巴西'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "44c20f3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'criska'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用键值对取值\n",
    "dict_01['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b952d98d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'19'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_01['age']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61b93c7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典键值对的增加\n",
    "dict_01['favorite_author']=\"maomu\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f83938e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'criska',\n",
       " 'age': '19',\n",
       " 'num': '2110120088',\n",
       " 'home': '巴西',\n",
       " 'favorite_author': 'maomu'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "25dfa15a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新列表频数\n",
    "found={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5df376f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "found['a']=0\n",
    "found['e']=0\n",
    "found['i']=0\n",
    "found['o']=0\n",
    "found['u']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "af51a4d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d81ca59d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e']=found['e']+1 \n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5587672e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e']+=1#表示递增，每个数值相差1，递减同理\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "16fed6fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d9035a68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a have found in  0 times\n",
      "e have found in  2 times\n",
      "i have found in  0 times\n",
      "o have found in  0 times\n",
      "u have found in  0 times\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k,'have found in ',found[k],'times')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "714094dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a have found in  0 times\n",
      "e have found in  2 times\n",
      "i have found in  0 times\n",
      "o have found in  0 times\n",
      "u have found in  0 times\n"
     ]
    }
   ],
   "source": [
    "# 让字典按照顺序输出\n",
    "for k in sorted(found):\n",
    "    print(k,'have found in ',found[k],'times')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "75f9b3a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a 0\n",
      "e 2\n",
      "i 0\n",
      "o 0\n",
      "u 0\n"
     ]
    }
   ],
   "source": [
    "# 利用items 在循环迭代中存在两个变量\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5497eba4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a 0\n",
      "e 2\n",
      "i 0\n",
      "o 0\n",
      "u 0\n"
     ]
    }
   ],
   "source": [
    "for k,v in found.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efcdb2ca",
   "metadata": {},
   "source": [
    "# P111 页习题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "82054b72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:kaka\n"
     ]
    }
   ],
   "source": [
    "vowels=['a','e','i','o','u']\n",
    "word=input(\"provide a word to search for vowels:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a07d8b82",
   "metadata": {},
   "outputs": [],
   "source": [
    "found={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6c51ac46",
   "metadata": {},
   "outputs": [],
   "source": [
    "found['a']=0\n",
    "found['e']=0\n",
    "found['i']=0\n",
    "found['o']=0\n",
    "found['u']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e6f27a6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "17c823b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a have found 2 times\n",
      "e have found 0 times\n",
      "i have found 0 times\n",
      "o have found 0 times\n",
      "u have found 0 times\n"
     ]
    }
   ],
   "source": [
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter in found:\n",
    "            found[letter]+=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'have found',v,'times')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32835600",
   "metadata": {},
   "source": [
    "# 动态赋键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "eb81ad39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits={}\n",
    "fruits[\"apples\"]=10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b79c2d62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"apple\"in fruits"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
